﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.BLL;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.Common;
using ZX.CRM.Library;
using ZX.Web.UI.Controls.ControlBind;

namespace ZX.WebApp.Crm.Deliver
{
    public partial class AddDeliverMajor : EditUIPage
    {
        BTCrmCustomer bll = new BTCrmCustomer();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BCrmDeliverMajor bllDeliverMajor = new BCrmDeliverMajor();
        BCrmProductLayout bllLayout = new BCrmProductLayout();
        string strListDetailHouseType = "[]",
               strListDetailTrafficType = "[]",
               strDeliverMajorInfo = "[]", strMinZu = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.UserId == null)
            {
                Response.Redirect("../../Error.aspx?message=1&functionId=" + FunctionID);
            }

            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);

            #region ------------ jsonData ------------
            //房型
            IList<DatPhraseDetail> ListDetailHouseType = bllDetail.GetDatPhraseDetailByClassValue("HouseType");
            if (ListDetailHouseType != null && ListDetailHouseType.Count > 0)
            {
                IList<BaseEntity> _listHouseType = new List<BaseEntity>();
                foreach (var _e in ListDetailHouseType)
                {
                    _listHouseType.Add(_e);
                }
                strListDetailHouseType = _listHouseType.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //交通方式
            IList<DatPhraseDetail> ListDetailTrafficType = bllDetail.GetDatPhraseDetailByClassValue("TrafficType");
            if (ListDetailTrafficType != null && ListDetailTrafficType.Count > 0)
            {
                IList<BaseEntity> _listTrafficType = new List<BaseEntity>();
                foreach (var _e in ListDetailTrafficType)
                {
                    _listTrafficType.Add(_e);
                }
                strListDetailTrafficType = _listTrafficType.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            //民族
            IList<DatPhraseDetail> ListMinZu = bllDetail.GetDatPhraseDetailByClassValue("MinZu");
            if (ListMinZu != null && ListMinZu.Count > 0)
            {
                IList<BaseEntity> _ListMinZu = new List<BaseEntity>();
                foreach (var _e in ListMinZu)
                {
                    _ListMinZu.Add(_e);
                }
                strMinZu = _ListMinZu.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
            }
            #endregion

            #region ------ dropdownlist ------
            //交付单状态
            IList<DatPhraseDetail> ListDliverId = bllDetail.GetDatPhraseDetailByClassValue("DeliverState");
            DropDownListControl.DropDataSourceBind(this.DliverState, ListDliverId, "PhraseText", "PhraseID");
            #endregion


            if (Flag == "0")
            {
                this.Code.Text = IDMaker.GetSequenceStr("DEL001");
                this.AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.ID.Value = Guid.NewGuid().ToString();
                this.txtAddOrderUser.Value = this.User.UName;
                this.txtAddOrderTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
            }
            else if (Flag == "1" || Flag == "2")
            {
                IList<V_CrmDeliverMajorInfo> instanceMajor = new List<V_CrmDeliverMajorInfo>();
                string strId = this.GetStringFromQueryString("id", "");
                if (strId == "")
                {
                    strId = this.GetStringFromQueryString("WorkflowInsId", "");
                    instanceMajor = bllDeliverMajor.GetAllDeliverMajorInfoByWorkFlowInsId(new Guid(strId));
                }
                else
                {
                    instanceMajor = bllDeliverMajor.GetAllDeliverMajorInfo(new Guid(strId));
                }
                if (instanceMajor != null && instanceMajor.Count > 0)
                {
                    this.ID.Value = instanceMajor[0].ID.ToString();
                    this.Code.Text = instanceMajor[0].Code;
                    this.AddTime.Text = instanceMajor[0].AddTime.ToString("yyy-MM-dd");
                    this.ProductLayoutID.Value = instanceMajor[0].ProductLayoutID.ToString();
                    this.OrderID.Value = instanceMajor[0].OrderID.ToString();
                    this.txtOrderID.Value = instanceMajor[0].OrderCode;
                    this.OrderProductID.Value = instanceMajor[0].OrderProductID.ToString();
                    this.CustomerName.Value = instanceMajor[0].CustomerName;
                    this.Account.Value = instanceMajor[0].Amount.ToString();
                    this.Pay1.Value = instanceMajor[0].AccountReceivable.ToString();
                    this.Pay2.Value = instanceMajor[0].ResignMoney.ToString();
                    this.CustomerSource.Value = instanceMajor[0].CustomerOrigin;
                    this.txtNegotiateTeach.Value = instanceMajor[0].NegotiateTeacherName;
                    this.NegotiateTeach.Value = instanceMajor[0].NegotiateTeach.ToString();
                    this.txtClassServer.Value = instanceMajor[0].ClassServerName;
                    this.ClassServer.Value = instanceMajor[0].ClassServer.ToString();
                    this.txtClassFrontServer.Value = instanceMajor[0].ClassFrontServerName;
                    this.ClassFrontServer.Value = instanceMajor[0].ClassFrontServer.ToString();
                    this.txtAddOrderUser.Value = instanceMajor[0].AddUserName;
                    this.txtAddOrderTime.Value = instanceMajor[0].AddTime.ToString();
                    this.txtProductLayoutID.Value = instanceMajor[0].CourseLayoutName;
                    string strBeginTime = instanceMajor[0].BeginDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].BeginDate.ToString("yyyy-MM-dd");
                    string strEndTime = instanceMajor[0].EndDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].EndDate.ToString("yyyy-MM-dd");
                    if (strBeginTime != "" || strEndTime != "")
                    {
                        this.Date.Value = strBeginTime + " -- " + strEndTime;
                    }
                    this.Address.Value = instanceMajor[0].Place;


                    IList<V_CrmProductLayout> listLayout = bllLayout.GetProductLayoutByProductLayoutId(instanceMajor[0].ProductLayoutID);
                    if (listLayout != null && listLayout.Count > 0)
                    {
                        for (int i = 0; i < listLayout.Count; i++)
                        {
                            if (listLayout[i].ProductRoleID.ToString() == "14BADCBA-3B89-47FD-9FEE-B62715FD33D0" || listLayout[i].ProductRoleName == "会务签到负责人")
                            {
                                this.SignPeople.Value = listLayout[i].ProductRoleUserName;
                            }
                            if (listLayout[i].ProductRoleID.ToString() == "1E10CA7D-D04F-4136-925C-19EC01A90118" || listLayout[i].ProductRoleName == "会务总负责人")
                            {
                                this.AllSignPeople.Value = listLayout[i].ProductRoleUserName;
                            }
                        }
                    }
                    this.GradeDeptId.Value = instanceMajor[0].GradeDeptId.ToString();
                    this.txtGradeDeptId.Value = instanceMajor[0].GradeDeptName;
                    this.ConstDeptId.Value = instanceMajor[0].ConstDeptId.ToString();
                    this.txtConstDeptId.Value = instanceMajor[0].ConstDeptName;
                    this.CustomerId.Value = instanceMajor[0].CustomerId.ToString();
                    this.CustomerMgr.Value = instanceMajor[0].CustomerMgr.ToString();
                    this.txtCustomerMgr.Value = instanceMajor[0].CustomerMgrName.ToString();
                    this.CustomerDirector.Value = instanceMajor[0].CustomerDirector.ToString();
                    this.txtCustomerDirector.Value = instanceMajor[0].CustomerDirectorName.ToString();
                    this.CustomerManager.Value = instanceMajor[0].CustomerManager.ToString();
                    this.txtCustomerManager.Value = instanceMajor[0].CustomerManagerName.ToString();

                    IList<BaseEntity> _listDeliverMajorInfo = new List<BaseEntity>();
                    foreach (var _e in instanceMajor)
                    {
                        _listDeliverMajorInfo.Add(_e);
                    }
                    strDeliverMajorInfo = _listDeliverMajorInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                    //交付单状态选中
                    DropDownListControl.DropDownListSelect(this.DliverState, instanceMajor[0].DliverState.ToString());
                    //是否现场完款选中
                    DropDownListControl.DropDownListSelect(this.exp3, instanceMajor[0].exp3);
                }
                if (strView == "m")
                {
                    this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                    this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
                }
                else if (strView == "c" || strView == "state")
                {
                    this.WorkFlowInsId.Value = instanceMajor[0].WorkFlowInsId.ToString();
                    this.WorkTaskInsId.Value = instanceMajor[0].WorkTaskInsId.ToString();
                }
                else
                {
                    this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                }
                this.AddUser.Value = this.UserId.ToString();
                this.txtAddUser.Value = this.User.UName;
                this.txtAddTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        #region-----------
        /// <summary>
        /// 弹出列表客户经理ID
        /// </summary>
        public string addUser { get { return this.UserId.ToString(); } }
        /// <summary>
        /// 标题
        /// </summary>
        public string PageTitle
        { get { return this.GetStringFromQueryString("title", ""); } }
        /// <summary>
        /// 房型
        /// </summary>
        public string ListDetailHouseType
        { get { return strListDetailHouseType; } }
        /// <summary>
        /// 交通方式
        /// </summary>
        public string ListDetailTrafficType
        { get { return strListDetailTrafficType; } }
        /// <summary>
        /// 民族
        /// </summary>
        public string ListMinZu
        { get { return strMinZu; } }
        /// <summary>
        /// 类型状态
        /// </summary>
        public string Flag
        { get { return this.GetStringFromQueryString("flag", ""); } }
        public string Type
        { get { return this.GetStringFromQueryString("type",""); } }
        public string Fkey
        { get { return this.GetStringFromQueryString("key", ""); } }
        /// <summary>
        /// 信息集合
        /// </summary>
        public string ListDeliverMajorInfo
        { get { return this.strDeliverMajorInfo; } }

        /// <summary>
        /// 
        /// </summary>
        public string strWorkFlowId
        {
            get
            {
                string wfid = this.GetStringFromQueryString("WorkflowId", "");
                if (wfid == "")
                {
                    wfid = this.GetStringFromQueryString("wfid", "");
                }
                return wfid;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        {
            get
            {
                string wtid = this.GetStringFromQueryString("WorktaskId", "");
                if (wtid == "")
                {
                    wtid = this.GetStringFromQueryString("wtid", "");
                }
                return wtid;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskInsId
        { get { return this.GetStringFromQueryString("WorktaskInsId", ""); } }
        /// <summary>
        /// 退回标记
        /// </summary>
        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strOperStatus
        { get { return this.GetStringFromQueryString("OperStatus", ""); } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }
        #endregion
    }
}